package com.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gson.reflect.TypeToken;
import com.util.AESUtil;
import com.util.GsonUtil;
import com.util.HttpClientUtil;

public abstract class BaseAction {
	String openIdKey="openId";
	String userNameKey="userName";
	String userImgKey="userImg";
	String tokenKey="tokenKey";
//	private static String openid = "oH_OR0qUFU5eHNN9CvmSka-Mexe0";
	
	/**
	 * 工具方法 发送post请求
	 * @param mathod
	 * @param jsonData
	 * @param request
	 * @return
	 */
    protected String getHttpJson(String mathod,String jsonData,HttpServletRequest request,boolean needLogin){
//    	String memKey="9FB6A72C239F4D26A68C078BCDE06819";
    	String memKey=request.getParameter("memKey");
    	if(memKey!=null&&!"".equals(memKey)){
    		if(memKey.contains("#NO#NO#")){
    			memKey=memKey.replace("#NO#NO#", "");
    		}else{
    			setMemKey(request, memKey);
    		}

    	}
    	if(needLogin&&memKey==null){
    		String result=login(request);
    		if("gotoRegister".equals(result)){
    			return result;
    		}
    		memKey=result;
    	}
    	String registerName="";
    	//创建通用json
    	String json=HttpClientUtil.getComJson(registerName, memKey);
    	//合并传参json字符串
    	if(jsonData!=null&&!"".equals(jsonData)){
    		json=json.substring(0,json.length()-1)+","+jsonData.substring(1,jsonData.length());
    	}
		return HttpClientUtil.doPost(mathod, AESUtil.Encrypt(json));
	}
    /**
	 * 工具方法 登录
	 * @param request
	 * @return
	 */
    protected String login(HttpServletRequest request){
    	String memKey=getMemKey(request); //通过session 获取用户ID
    	if(memKey==null||"".equals(memKey)){
    		String openId=(String) getHttpSession(request).getAttribute(openIdKey);//获取OpenId
    		if(openId==null){//openId为空，需获得openId
    			return "gotoRegister";
    		}else{//重新登录
    			memKey=getMemKey(openId);
				if(memKey!=null&&!"".equals(memKey)){
					setMemKey(request,memKey);
				}else{
					return "gotoRegister";
				}
    		}
    	}
		return memKey;
	}
    
    protected void baseLoginOut(HttpServletRequest request){
    	getHttpSession(request).setAttribute("memKey",null);
    	getHttpSession(request).setAttribute(userNameKey,null);
    	getHttpSession(request).setAttribute(openIdKey,null);
    	getHttpSession(request).setAttribute(userImgKey,null);
	}
    
    /**
	 * 工具方法 登录
	 * @return
	 */
    protected String getMemKey(String openId){
    	String memKey="";
		Map<String,Object> promap=HttpClientUtil.getComMap("100","","");
		promap.put("OpenId", openId);
		String json=HttpClientUtil.doPost("Logistics/MEM/WeChatMemLogin", AESUtil.Encrypt(GsonUtil.toJson(promap)));
		if(json!=null){
			Map<String,Object> map=GsonUtil.fromJson(json,new TypeToken<Map<String, Object>>(){}.getType());
            if((Double)map.get("stateID")==0&&(Double)map.get("businessState")==0){
                memKey=(String) map.get("MemKey");
            }

		}
		return memKey;
	}

	/**
	 * 工具方法  获取session
	 * @param request
	 * @return
	 */
	protected HttpSession getHttpSession(HttpServletRequest request){
		HttpSession session= request.getSession(false);
		if(session==null){
			session= request.getSession(true);
		}
		return session;
	}
	/**
	 * 工具方法  获取session
	 * @param request
	 * @return
	 */
	protected HttpSession getHttpSession(HttpServletRequest request,boolean sessionType){
		return request.getSession(sessionType);
	}

	/**
	 * 工具方法  获取MemKey
	 * @param request
	 * @return
	 */
	protected String getMemKey(HttpServletRequest request){
		String memKey="";
		if(getHttpSession(request)!=null&&getHttpSession(request).getAttribute("memKey")!=null){
			memKey=(String) getHttpSession(request).getAttribute("memKey");
		}
		return memKey;
	}
	protected void setMemKey(HttpServletRequest request,String memKey){
		getHttpSession(request).setAttribute("memKey", memKey);
	}
}
